package com.yanqu.road.server.manger.activity.wealthscroll;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.wealthscroll.config.WealthScrollTaskConfig;
import com.yanqu.road.entity.enums.activity.wealthscroll.eWealthScrollBuyLevel;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.logic.activity.wealthscroll.WealthScrollConfig;
import com.yanqu.road.pb.activity.WealthScrollProto;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.WealthScrollModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;

import java.util.List;
import java.util.Map;

/**
 * 新财神密卷
 */

public class WealthScrollActivityMgr extends TempMgr {
    //配置
    private static WealthScrollConfig config;

    /**
     * 刷新
     */
    public static void reloadWealthScrollConfig() {
        getLogger().info("reload WealthScrollConfig start");
        List<ActivityInfo> openActivityInfo = NormalActivityMgr.getOpenActivityInfoList(eActivityType.WealthScroll.getValue());
        if (openActivityInfo.isEmpty()) {
            config = null;
            getLogger().info("reloadHorseRunConfig end openActivityInfo is empty");
            return;
        }
        ActivityInfo activityInfo = openActivityInfo.get(0);
        Map<String, ActivityConfig> activityConfigMap = ActivityMgr.getActivityConfigMap(activityInfo.getActivityId());
        WealthScrollConfig tmpConfig = new WealthScrollConfig(activityInfo, activityConfigMap);
        config = tmpConfig;
        getLogger().info("reloadWealthScrollConfig end config activityId{},最大期数{},最大复购等级{}",config.getActivityId(),config.getLoopEndNo(),config.getRepeatBuyMax());

        for (GamePlayer player : GamePlayerMgr.getAllOnlinePlayer()){
            player.getModule(WealthScrollModule.class).init();
        }
    }

    /**
     * 获取配置msg
     */
    public static WealthScrollProto.WealthScrollConfigSync.Builder getConfigMsgBuilder() {
        if (config == null) {
            return null;
        }
        WealthScrollProto.WealthScrollConfigSync.Builder builder = WealthScrollProto.WealthScrollConfigSync.newBuilder();
        for (WealthScrollTaskConfig taskConfig:config.getTaskConfigMap().values()) {
            WealthScrollProto.WealthScrollConfigMsg.Builder taskBuild = WealthScrollProto.WealthScrollConfigMsg.newBuilder();
            taskBuild.setId(taskConfig.getId());
            taskBuild.setCompleteType(taskConfig.getCompleteType());
            taskBuild.setCompleteParams(taskConfig.getCompleteParams());
            taskBuild.setRewards(taskConfig.getRewards());
            taskBuild.setIsScheduleRewards(taskConfig.getIsScheduleRewards());
            taskBuild.setIsSpecialRewards(taskConfig.getIsSpecialRewards());
            taskBuild.setIsSuperRewards(taskConfig.getIsSuperRewards());
            builder.addConfig(taskBuild);
        }
        return builder;
    }

    /**
     * 获取某一条任务的奖励 如果是进度奖励需要按期数拿
     */
    public static String getTaskRewardString(WealthScrollTaskConfig taskConfig,int no){
        String configRewards;
        if (taskConfig.getCompleteType() == eGamePlayerEventType.WealthScrollExpProcess.getValue()){
            configRewards = taskConfig.getNoRewards(no);
        } else {
            configRewards = taskConfig.getRewards();
        }
        return configRewards;
    }

    /**
     * 道具是否是新财神密卷的经验
     * @param goodId
     * @return
     */

    public static boolean isNewWealthScrollExp(int goodId){
        if (config == null) {
            return false;
        }
        return config.getExpItem() == goodId;
    }

    /**
     * 新财神密卷活动是否开启
     * @return
     */

    public static boolean isNewWealthScrollOpening(){
        if (config == null) {
            return false;
        }
        ActivityInfo activityInfo = config.getActivityInfo();
        if (activityInfo == null) {
            return false;
        }
        if (ActivityMgr.activityInShowTime(activityInfo)){
            return true;
        }else {
            return false;
        }
    }

    //找不到下一期配置后，从第n期开始循环
    public static int getNextWealthScrollNo(int no){
        int nextNo = no + 1;
        if (nextNo > config.getLoopEndNo()){
            nextNo = config.getLoopStartNo();
        }
        return nextNo;
    }

    public static WealthScrollConfig getConfig() {
        return config;
    }

    /**
     * 购买不同类型加的经验值
     * @param mallParam
     * @return
     */
    public static int buyAdvanceAddExp(int mallParam){
        if (config == null) {
            return 0;
        }
        int eachLevelExp = config.getEachLevelExp();
        if (mallParam == eWealthScrollBuyLevel.advance.getValue()){
            int advanceLevelUp = config.getAdvanceLevelUp();
            return advanceLevelUp * eachLevelExp;
        }else if (mallParam == eWealthScrollBuyLevel.luxury.getValue()){
            int advanceLevelUp = config.getLuxuryLevelUp();
            return advanceLevelUp * eachLevelExp;
        }else {
            getLogger().error("新财神密卷购买错误");
            return 0;
        }
    }

    /**
     * 获取经验道具id
     */
    public static int getScoreItemId(){
        if (config == null) {
            return 0;
        }
        return config.getScoreItem();
    }



    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

}
